package com.website.core.util;

import com.website.system.model.Visitor;

import java.util.*;

/**
 * ECharts生成JSON数据方法
 */
public class EChartsUtil {

    /**
     * 获取当前月及前n个月 数字
     *
     * @author yanqb
     * @Date 2019-11-01 12:55
     */
    private static int[] getNumberMonths(int number){
        Calendar date=Calendar.getInstance();
        int month=date.get(Calendar.MONTH)+1;

        int[] arr = new int[number];
        for (int i = 1; i <= arr.length; i++) {
            if(month>number){
                arr[i-1]=(month-number+i);
            }else{
                if(month-i<0){
                    arr[arr.length-i]=(month-i+13);
                }else{
                    arr[arr.length-i]=(month-i+1);
                }
            }
        }
        return arr;
    }

    /**
     * 转换文章发布量 数据   方法二：
     *
     * @author yanqb
     * @Date 2019-11-01 12:55
     */
    public static List<String> getArticleCountMap(List<Visitor> list,int type){

        // monthAll有6个月份，monthType可能不够6个月份。所以对比俩组数据把没有的月份数据设置为0
        int[] monthAll = getNumberMonths(6);
        List<String> monthType = new ArrayList<>();
        Map<String,String> map = new HashMap<>();

        for(Visitor visitor:list){
            if(visitor.getArticleType().equals(String.valueOf(type))){
                monthType.add(visitor.getMonth());
                map.put(visitor.getMonth(),visitor.getSum());
            }
        }
        List<String> articleCount = new ArrayList<>();
        for(int month:monthAll){
            if(monthType.contains(String.valueOf(month))){
                articleCount.add(map.get(String.valueOf(month)));
            }else{
                articleCount.add("0");
            }
        }
        return articleCount;
    }

    public static void main(String[] args) {
        List<Visitor> list = new ArrayList<>();
        for(int i=3; i<=8; i++){
            if(i!=5){
                Visitor visitor = new Visitor();
                visitor.setArticleType("1");
                visitor.setMonth(String.valueOf(i));
                visitor.setSum(String.valueOf(new Random().nextInt(99)));
                list.add(visitor);
            }
        }
        for(int i=3; i<=5; i++){
            Visitor visitor = new Visitor();
            visitor.setArticleType("2");
            visitor.setMonth(String.valueOf(i));
            visitor.setSum(String.valueOf(new Random().nextInt(99)));
            list.add(visitor);
        }
        getArticleCountMap(list,1);
        getEnglishMonths(6);
    }

    /**
     * 对应中文
     * @author yanqb
     * @Date 2019-11-01 12:55
     */
    public static List<String> getChineseMonths(int number){
        List<String> list = new ArrayList<>();
        int[] arr = getNumberMonths(number);
        Map<Integer,String> monthMap = new HashMap<>();
        monthMap.put(1,"1月");
        monthMap.put(2,"2月");
        monthMap.put(3,"3月");
        monthMap.put(4,"4月");
        monthMap.put(5,"5月");
        monthMap.put(6,"6月");
        monthMap.put(7,"7月");
        monthMap.put(8,"8月");
        monthMap.put(9,"9月");
        monthMap.put(10,"10月");
        monthMap.put(11,"11月");
        monthMap.put(12,"12月");
        for(int i:arr){
            list.add(monthMap.get(i));
        }
        System.out.println("----中文月数:    " + list.toString());
        return list;
    }

    /**
     * 对应英文
     * @author yanqb
     * @Date 2019-11-01 12:55
     */
    public static List<String> getEnglishMonths(int number){
        List<String> list = new ArrayList<>();
        int[] arr = getNumberMonths(number);
        Map<Integer,String> monthMap = new HashMap<>();
        monthMap.put(1,"Jan");
        monthMap.put(2,"Feb");
        monthMap.put(3,"Mar");
        monthMap.put(4,"Apr");
        monthMap.put(5,"May");
        monthMap.put(6,"Jun");
        monthMap.put(7,"Jul");
        monthMap.put(8,"Aug");
        monthMap.put(9,"Sep");
        monthMap.put(10,"Oct");
        monthMap.put(11,"Nov");
        monthMap.put(12,"Dec");
        for(int i:arr){
            list.add(monthMap.get(i));
        }
        System.out.println("----英文月数:    " + list.toString());
        return list;
    }


}